package com.ry.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.common.CommonResult;
import com.ry.model.dto.QueryState;
import com.ry.model.dto.StateDTO;
import com.ry.model.dto.StateDetailDTO;
import com.ry.service.StateService;
import com.ry.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

/**
 * @author ry
 * @since 2021-11-11 10:36
 **/
@Slf4j
@RequestMapping("/state")
@RestController
public class StateController {
    @Autowired
    private StateService stateService;

    @PostMapping("/publish")
    public CommonResult<Void> publish(@RequestBody String jsonParam) throws UnsupportedEncodingException {
        JSONObject jsonObject = JSONObject.parseObject(jsonParam);
        String stateDTOJson = jsonObject.getString("data");
        StateDTO stateDTO = JSONUtil.toBean(stateDTOJson, StateDTO.class);
        log.info(stateDTO.toString());
        //stateDto里面的内容也是utf8编码的，为什么数据库会出现乱码?配置文件写错了url那里大小写严格区分
        stateService.publish(stateDTO);

        return ResultUtil.success();
    }

    @PostMapping("/selectPage")
    public CommonResult<Page<StateDetailDTO>> selectPage(QueryState queryState){
        Page<StateDetailDTO> stateDetailDTOPage = stateService.selectByPage(queryState);

        return ResultUtil.success(stateDetailDTOPage);
    }
}
